home *** CD-ROM | disk | FTP | other *** search
- //-----------------------------------------
- // ImagePanel.cs ⌐ 2001 by Charles Petzold
- //-----------------------------------------
- using System;
- using System.Drawing;
- using System.IO;
- using System.Windows.Forms;
-
- class ImagePanel: Panel
- {
- const int cxButton = 100, cyButton = 100; // Tama±o del bot≤n de imagen
-
- Button btnClicked;
- ToolTip tooltip = new ToolTip();
- Timer timer = new Timer();
-
- // Campos para el evento Tick de Timer
-
- string[] astrFileNames;
- int i, x, y;
- // Evento p·blico
- public event EventHandler ImageClicked;
- // Constructor
- public ImagePanel()
- {
- AutoScroll = true;
-
- timer.Interval = 1;
- timer.Tick += new EventHandler(TimerOnTick);
- }
- // Propiedades p·blicas
- public Control ClickedControl
- {
- get { return btnClicked; }
- }
- public Image ClickedImage
- {
- get
- {
- try
- {
- return Image.FromFile((string) btnClicked.Tag);
- }
- catch
- {
- return null;
- }
- }
- }
- // MΘtodo p·blico
- public void ShowImages(string strDirectory)
- {
- Controls.Clear();
- tooltip.RemoveAll();
-
- try
- {
- astrFileNames = Directory.GetFiles(strDirectory);
- }
- catch
- {
- return;
- }
-
- i = x = y = 0;
-
- timer.Start();
- }
- // Manejadores de evento
- void TimerOnTick(object obj, EventArgs ea)
- {
- Image image;
-
- if (i == astrFileNames.Length)
- {
- timer.Stop();
- return;
- }
- try
- {
- image = Image.FromFile(astrFileNames[i]);
- }
- catch
- {
- i++;
- return;
- }
- int cxImage = image.Width;
- int cyImage = image.Height;
-
- // Convertir imagen a tama±o peque±o para el bot≤n.
-
- SizeF sizef = new SizeF(cxImage / image.HorizontalResolution,
- cyImage / image.VerticalResolution);
-
- float fScale = Math.Min(cxButton / sizef.Width,
- cyButton / sizef.Height);
- sizef.Width *= fScale;
- sizef.Height *= fScale;
- Size size = Size.Ceiling(sizef);
- Bitmap bitmap = new Bitmap(image, size);
- image.Dispose();
-
- // Crear bot≤n y a±adir al panel.
-
- Button btn = new Button();
- btn.Image = bitmap;
- btn.Location = new Point(x, y) + (Size) AutoScrollPosition;
- btn.Size = new Size(cxButton, cyButton);
- btn.Tag = astrFileNames[i];
- btn.Click += new EventHandler(ButtonOnClick);
- Controls.Add(btn);
-
- // Dar una Informaci≤n en pantalla al bot≤n
-
- tooltip.SetToolTip(btn, String.Format("{0}\n{1}x{2}",
- Path.GetFileName(astrFileNames[i]),
- cxImage, cyImage));
-
- // Ajustar i, x, e y para la imagen siguiente.
-
- AdjustXY(ref x, ref y);
- i++;
- }
- void ButtonOnClick(object obj, EventArgs ea)
- {
- btnClicked = (Button) obj;
-
- if (ImageClicked != null)
- ImageClicked(this, EventArgs.Empty);
- }
- protected override void OnResize(EventArgs ea)
- {
- base.OnResize(ea);
-
- AutoScrollPosition = Point.Empty;
- int x = 0, y = 0;
-
- foreach (Control cntl in Controls)
- {
- cntl.Location = new Point(x, y) + (Size) AutoScrollPosition;
- AdjustXY(ref x, ref y);
- }
- }
- void AdjustXY(ref int x, ref int y)
- {
- y += cyButton;
-
- if (y + cyButton > Height -
- SystemInformation.HorizontalScrollBarHeight)
- {
- y = 0;
- x += cxButton;
- }
- }
- }
-